package cn.tiaf

import caiquan.{Computer, User}

import scala.io.StdIn
import scala.util.control.Breaks.{break, breakable}

class Game() {
  var user: User = _
  var computer: Computer = _
  var Times: Int = _
  var IsContinue: Boolean = _
  val shoushi = Array("剪刀", "石头", "布")

  def init(): Unit = {
    computer = new Computer()
    user = new User()
    println("-----------------欢迎进入游戏世界-------------------")
    println("*************************************************")
    println("*******************猜拳,开始**********************")
    println("*************************************************")
    computer.init()
    user.name = "游客"
  }

  def start(): Unit = {
    breakable {
      while (true) {
        if (!IsContinue) {
          println(s"您选择了与${computer.name}对战,要开始吗?y/n")
          IsContinue = try {
            StdIn.readBoolean()
          } catch {
            case ex: Exception =>
              println("输入不符合规范,不能开始")
              false
          }
        }

        if (IsContinue) {
          //比较大小
          val userResult = user.showFirst(shoushi.size)
          val computerResult = computer.showFirst(shoushi.length)
          println(s"你出拳:${shoushi(userResult - 1)}")
          println(s"${computer.name}出拳!")
          println(s"${computer.name}出拳:${shoushi(computerResult - 1)}")

          if (userResult == computerResult) {
            user.score += 1
            computer.score += 1
            println("结果:和局! 下次继续努力")
            user.heju += 1
            computer.heju += 1
          }
          //1.剪刀 2.石头 3.布
          if ((userResult == 1 && computerResult == 3) || (userResult == 2 && computerResult == 1) || (userResult == 3 && computerResult == 2)) {
            user.score += 2
            println("结果:恭喜你赢啦!")
            user.shengju += 1
            computer.fuju += 1
          }
          if ((computerResult == 1 && userResult == 3) || (computerResult == 2 && userResult == 1) || (computerResult == 3 && userResult == 2)) {
            computer.score += 2
            println("结果:输啦!")
            computer.shengju += 1
            user.fuju += 1
          }
          Times += 1
          //输出结果
          println("是否开启下一轮?y/n")
          IsContinue = try {
            StdIn.readBoolean()
          } catch {
            case ex: Exception => false
          }
          if (!IsContinue) break()
        }
        else {
          break()
        }
      }
    }
    display()
  }

  def display(): Unit = {
    println("退出游戏!")
    println("-------------------------------------------------")
    println(s"${
      computer.name
    } VS ${
      user.name
    }")
    println(s"对战次数 ${Times} 次")
    println()
    println()
    println()
    println("姓名    得分    胜局    和局    负局")
    println(s"${
      user.name
    }    ${
      user.score
    }     ${
      user.shengju
    }    ${
      user.heju
    }    ${
      user.fuju
    }")
    println(s"${
      computer.name
    }    ${
      computer.score
    }     ${
      computer.shengju
    }    ${
      computer.heju
    }    ${
      computer.fuju
    }")
  }
}

object Game {
  def main(args: Array[String]): Unit = {
    val game = new Game()
    game.init()
    game.start()
  }
}
